library(magrittr)
library(tidyverse)
library(Seurat)
library(readxl)
library(cowplot)
library(colorblindr)
library(viridis)
library(magick, lib.loc = "/home/uhlitzf/miniconda3/lib/R/library")
library(ggpubr)

1 Overview

## load global vars: 
source("_src/global_vars.R")

# meta_tbl
# clrs
# markers_v6
# markers_v6_super
# cell_type_super_lookup

names(clrs$cell_type) <- str_replace_all(names(clrs$cell_type), "\\.", " ")
names(clrs$cell_type) <- str_replace_all(names(clrs$cell_type), "Ovarian", "Ov")

## load data --------------------------------------

## load cohort embeddings
seu_tbl <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/freeze/v6/outs_pre/cells.tsv") %>% 
  mutate(cell_type = ifelse(cell_type == "Monocyte", "Myeloid.cell", cell_type)) %>% 
  mutate(cell_type = ifelse(cell_type == "Ovarian.cancer.cell", "Ov.cancer.cell", cell_type))

## load consensus data
consOV_tbl <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/freeze/v6/consensusOV/SPECTRUM_freeze_v6_consensusOV.tsv") %>%
  mutate(consensusOV = ordered(consensusOV, levels = names(clrs$consensusOV))) %>% 
  select(cell_id, consensusOV)

## join data
seu_tbl_full <- seu_tbl %>% 
  left_join(meta_tbl, by = "sample") %>% 
  filter(therapy == "pre-Rx", cell_type != "Other", tumor_supersite != "Unknown") %>% 
  rename(UMAP_1 = umap50_1, UMAP_2 = umap50_2) %>% 
  mutate(cell_type_super = cell_type_super_lookup[cell_type]) %>% 
  mutate(cell_type = ordered(str_replace_all(cell_type, "\\.", " "), 
                             levels = names(clrs$cell_type))) %>% 
  mutate(sort_short_x = ifelse(sort_short == "U" & cell_type_super == "Immune", 
                               "CD45+", ifelse(sort_short == "U" & cell_type_super == "Stromal", 
                                               "CD45-", sort_short))) %>% 
  left_join(consOV_tbl, by = "cell_id")

# seu_tbl_full <- seu_tbl_full %>%
#   sample_n(10000)

2 cell type compositions scRNA

source("_src/comp_plot.R")
# rank_by()

comp_tbl_sample <- seu_tbl_full %>%
  filter(therapy == "pre-Rx", cell_type != "Other") %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature, cell_type) %>%
  tally() %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature) %>%
  mutate(nrel = n/sum(n)*100) %>% 
  mutate(label_supersite = "Site",
         label_therapy = "Rx",
         label_mutsig = "Signature") %>%
  mutate(tumor_supersite = ordered(tumor_supersite, levels = rev(names(clrs$tumor_supersite)))) %>%
  mutate(sample_id = paste(tumor_subsite, patient_id_short, therapy, sort_short_x)) %>%
  group_by(sample_id) %>% 
  mutate(ntotal = sum(n)) %>% 
  filter(ntotal > 0) %>% 
  ungroup()


comp_tbl_consOV <- seu_tbl_full %>%
  filter(therapy == "pre-Rx", cell_type != "Other") %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature, consensusOV) %>%
  tally %>%
  group_by(tumor_subsite, tumor_supersite, tumor_megasite, patient_id_short,
           therapy, sort_short_x, consensus_signature) %>%
  mutate(nrel = n/sum(n)*100,
         log10n = log10(n)) %>% 
  mutate(tumor_supersite = ordered(tumor_supersite, levels = rev(names(clrs$tumor_supersite)))) %>%
  mutate(sample_id = paste(tumor_subsite, patient_id_short, therapy, sort_short_x)) %>%
  ungroup


# 
# g9 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                         consensusOV, "Immunoreactive")
# g10 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                          consensusOV, "Mesenchymal")
# g11 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                          consensusOV, "Differentiated")
# g12 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45-"),
#                          consensusOV, "Proliferative")
# 
# g13 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Immunoreactive")
# g14 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Mesenchymal")
# g15 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Differentiated")
# g16 <- default_comp_grid(filter(comp_tbl_consOV, sort_short_x == "CD45+"),
#                          consensusOV, "Proliferative")

# pdf("_fig/002_cohort/002_comp_full.pdf", width = 3.5, height = 12)
# g1;g2;g3;g4;g5;g6;g7;g8;g9;g10;g11;g12;g13;g14;g15;g16
# dev.off()

2.1 Site

cell_types_immune <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Immune"]), "\\.", " ")
cell_types_stromal <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Stromal"]), "\\.", " ") %>% 
  str_replace_all("Ovarian", "Ov")
  
for(i in 1:length(cell_types_immune)){
  
  cat('### ', cell_types_immune[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_sample, sort_short_x == "CD45+"), 
                                  cell_type, cell_types_immune[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox2, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

2.1.1 B cell

2.1.2 Plasma cell

2.1.3 T cell

2.1.4 Myeloid cell

2.1.5 Dendritic cell

for(i in 1:length(cell_types_stromal)){
  
  cat('### ', cell_types_stromal[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_sample, sort_short_x == "CD45-"), 
                                  cell_type, cell_types_stromal[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox2, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

2.1.6 Endothelial cell

2.1.7 Fibroblast

2.1.8 Ov cancer cell

2.2 Signature

cell_types_immune <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Immune"]), "\\.", " ")
cell_types_stromal <- str_replace_all(names(cell_type_super_lookup[cell_type_super_lookup=="Stromal"]), "\\.", " ") %>% 
  str_replace_all("Ovarian", "Ov")
  
for(i in 1:length(cell_types_immune)){
  
  cat('### ', cell_types_immune[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_sample, sort_short_x == "CD45+"), 
                                  cell_type, cell_types_immune[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox1, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

2.2.1 B cell

2.2.2 Plasma cell

2.2.3 T cell

2.2.4 Myeloid cell

2.2.5 Dendritic cell

for(i in 1:length(cell_types_stromal)){
  
  cat('### ', cell_types_stromal[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_sample, sort_short_x == "CD45-"), 
                                  cell_type, cell_types_stromal[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox1, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

2.2.6 Endothelial cell

2.2.7 Fibroblast

2.2.8 Ov cancer cell

3 TCGA compositions scRNA

3.1 Site

tcga_subtypes <- as.character(unique(consOV_tbl$consensusOV))

for(i in 1:length(tcga_subtypes)){
  
  cat('### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_consOV, sort_short_x == "CD45+"), 
                                  consensusOV, tcga_subtypes[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox2, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

3.1.1 Differentiated

3.1.2 Immunoreactive

3.1.3 Mesenchymal

3.1.4 Proliferative

for(i in 1:length(tcga_subtypes)){
  
  cat('### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_consOV, sort_short_x == "CD45-"), 
                                  consensusOV, tcga_subtypes[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox2, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

3.1.5 Differentiated

3.1.6 Immunoreactive

3.1.7 Mesenchymal

3.1.8 Proliferative

3.2 Signature

tcga_subtypes <- as.character(unique(consOV_tbl$consensusOV))

for(i in 1:length(tcga_subtypes)){
  
  cat('### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_consOV, sort_short_x == "CD45+"), 
                                  consensusOV, tcga_subtypes[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox1, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')
  
}

3.2.1 Differentiated

3.2.2 Immunoreactive

3.2.3 Mesenchymal

3.2.4 Proliferative

for(i in 1:length(tcga_subtypes)){
  
  cat('### ', tcga_subtypes[i],' \n')
  
  plist <- default_comp_grid_list(filter(comp_tbl_consOV, sort_short_x == "CD45-"), 
                                  consensusOV, tcga_subtypes[i])
  
  p <- plot_grid(plist$pbar1, plist$pbar2, plist$pbox1, plist$pvec, 
                 ncol = 1, align = "v",
                 rel_heights = c(0.13, 0.13, 0.13, 0.61))
  
  print(p)
  
  cat(' \n \n')

}

3.2.5 Differentiated

3.2.6 Immunoreactive

3.2.7 Mesenchymal

3.2.8 Proliferative

4 summary stats grid

# right_grid_full <- ggdraw() +
#   draw_plot(tcga_heat + guides(fill = F), x = -0.05, y = 0.58, width = 0.65, height = 0.4) +
#   draw_grob(tcga_heat_legend, x = 0.05, y = 0.56, width = 0.2, height = 0.2) +
#   draw_plot(mixing_plot_cell_type, x = 0.56, y = 0.58, width = 0.45, height = 0.4) +
#   draw_plot(pcomp_grid_p_full, x = 0, y = 0, width = 0.5, height = 0.55) +
#   draw_plot(pcomp_grid_n_full, x = 0.5, y = 0, width = 0.5, height = 0.55)
#   
# right_grid_full
# 
# ggsave("_fig/001x_right_grid_full.png", right_grid_full, width = 6, height = 10)
# ggsave("_fig/001x_right_grid_full.pdf", right_grid_full, width = 6, height = 10)
# right_grid <- ggdraw() +
#   draw_plot(tcga_heat + guides(fill = F), x = -0.05, y = 0.65, width = 0.65, height = 0.35) +
#   draw_grob(tcga_heat_legend, x = 0.05, y = 0.6, width = 0.2, height = 0.2) +
#   draw_plot(mixing_plot_cell_type, x = 0.56, y = 0.65, width = 0.45, height = 0.35) +
#   draw_plot(pcomp_grid_p, x = 0, y = 0, width = 0.5, height = 0.6) +
#   draw_plot(pcomp_grid_n, x = 0.5, y = 0, width = 0.5, height = 0.6)
#   
# right_grid
# 
# ggsave("_fig/001x_right_grid.png", right_grid, width = 6, height = 10)
# ggsave("_fig/001x_right_grid.pdf", right_grid, width = 6, height = 10)

4.1 mpIF composition

# ## meta data for mpIF
# mpif_meta_tbl <- read_excel("_data/small/MSK SPECTRUM - mpIF.xlsx", sheet = 3) %>%
#   mutate(slide_id = str_replace_all(pici_id, " ", "_"),
#          sample_cd45p = paste0(patient_id, "_", surgery, "_CD45P_", str_replace_all(toupper(tumor_subsite), " ", "_")),
#          sample_cd45n = paste0(patient_id, "_", surgery, "_CD45N_", str_replace_all(toupper(tumor_subsite), " ", "_"))) %>%
#   mutate(tumor_supersite = ifelse(tumor_supersite == "Upper Quadrant", "UQ", tumor_supersite))
# 
# # mpif_pixel <- read_tsv("/work/shah/vazquezi/data/transfers/spectrum/results/mpif/v8/integrate/outputs/cohort_merge/patient/SPECTRUM/all/detection.tsv") %>%
# #   select(cell_id, compartment = Parent)
# #
# # mpif_cell_type <- read_tsv("/work/shah/vazquezi/data/transfers/spectrum/results/mpif/v8/integrate/outputs/cohort_merge/patient/SPECTRUM/all/cell_type_manual.tsv") %>%
# #   left_join(mpif_pixel, by = "cell_id") %>%
# #   mutate(cell_id = str_remove_all(cell_id, "CD68.TOX.PD1.PDL1.CD8.panCK_CK8-18.DAPI_|_component_data - resolution #1")) %>%
# #   separate(cell_id, into = c("patient_id", "tumor_subsite", "fov_id", "cell_idx"),
# #            sep = "_", remove = F) %>%
# #   mutate(fov_id = paste0(patient_id, "_", tumor_subsite, "_", fov_id),
# #          slide_id = paste0(patient_id, "_", tumor_subsite))
# #
# # ## expand for double and triple positive cells (cell type markers)
# # mpif_ncell <- mpif_cell_type %>%
# #   # sample_n(10000) %>%
# #   mutate(CD68_state_log = CD68_state == "CD68+",
# #          CD8_state_log = CD8_state == "CD8+",
# #          panCK_state_log = panCK_state == "panCK+") %>%
# #   group_by(cell_id) %>%
# #   mutate(n_cells = sum(CD68_state_log, CD8_state_log, panCK_state_log)) %>%
# #   select(cell_id, n_cells) %>%
# #   deframe
# #
# # mpif_cell_type_expanded <- bind_rows(
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 0]),
# #          TOX_state == "TOX+" | PD1_state == "PD1+" | PDL1_state == "PDL1+") %>%
# #     mutate(cell_id = paste0(cell_id, "_0"),
# #            cell_type = c("Other")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 0]),
# #          TOX_state == "TOX-" & PD1_state == "PD1-" & PDL1_state == "PDL1-") %>%
# #     mutate(cell_id = paste0(cell_id, "_0"),
# #            cell_type = c("Unknown")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 1]),
# #          CD8_state == "CD8+") %>%
# #     mutate(cell_id = paste0(cell_id, "_1"),
# #            cell_type = c("CD8+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 1]),
# #          CD68_state == "CD68+") %>%
# #     mutate(cell_id = paste0(cell_id, "_2"),
# #            cell_type = c("CD68+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 1]),
# #          panCK_state == "panCK+") %>%
# #     mutate(cell_id = paste0(cell_id, "_3"),
# #            cell_type = c("panCK+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 2]),
# #          CD8_state == "CD8+") %>%
# #     mutate(cell_id = paste0(cell_id, "_1"),
# #            cell_type = c("CD8+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 2]),
# #          CD68_state == "CD68+") %>%
# #     mutate(cell_id = paste0(cell_id, "_2"),
# #            cell_type = c("CD68+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 2]),
# #          panCK_state == "panCK+") %>%
# #     mutate(cell_id = paste0(cell_id, "_3"),
# #            cell_type = c("panCK+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 3]),
# #          CD8_state == "CD8+") %>%
# #     mutate(cell_id = paste0(cell_id, "_1"),
# #            cell_type = c("CD8+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 3]),
# #          CD68_state == "CD68+") %>%
# #     mutate(cell_id = paste0(cell_id, "_2"),
# #            cell_type = c("CD68+")),
# #   filter(mpif_cell_type, cell_id %in% names(mpif_ncell[mpif_ncell == 3]),
# #          panCK_state == "panCK+") %>%
# #     mutate(cell_id = paste0(cell_id, "_3"),
# #            cell_type = c("panCK+"))
# # )
# #
# # mpif_cell_state_expanded <- mpif_cell_type_expanded %>%
# #   mutate(cell_state = case_when(
# #     cell_type == "CD8+" ~ paste0(CD8_state, TOX_state, PD1_state),
# #     cell_type == "CD68+" ~ paste0(CD68_state, PDL1_state),
# #     cell_type == "panCK+" ~ paste0(panCK_state, PDL1_state),
# #     cell_type == "Unknown" ~ "Unknown",
# #     cell_type == "Other" ~ "Other"
# #   ))
# #
# # write_tsv(mpif_cell_state_expanded, "/work/shah/uhlitzf/data/SPECTRUM/mpIF/cell_type_manual_expanded.tsv")
# 
# mpif_cell_state_expanded <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/mpIF/cell_type_manual_expanded.tsv") %>%
#   select(cell_id, slide_id, fov_id, compartment, cell_type, cell_state, contains("state")) %>%
#   left_join(select(mpif_meta_tbl, slide_id, patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, sample_cd45n), by = "slide_id") %>%
#   na.omit()
# 
# # ## cell type composition
# # mpif_cell_type_n <- mpif_cell_state_expanded %>%
# #   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, fov_id, cell_type) %>%
# #   tally() %>%
# #   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, fov_id) %>%
# #   mutate(nrel = n/sum(n)) %>%
# #   ungroup()
# #
# # ## cell type composition slide lvl
# # mpif_cell_type_n_slide <- mpif_cell_state_expanded %>%
# #   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, cell_type) %>%
# #   tally() %>%
# #   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p) %>%
# #   mutate(nrel = n/sum(n)) %>%
# #   ungroup()
# 
# ## cell state composition fov lvl
# mpif_cell_state_n <- mpif_cell_state_expanded %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, fov_id, cell_state, cell_type) %>%
#   tally() %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, fov_id) %>%
#   mutate(nrel = n/sum(n)) %>%
#   ungroup()
# 
# ## cell state composition slide lvl
# mpif_cell_state_n_slide <- mpif_cell_state_expanded %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, cell_state, cell_type) %>%
#   tally() %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p) %>%
#   mutate(nrel = n/sum(n)) %>%
#   ungroup()
# 
# ## cell state composition slide lvl compartment
# mpif_cell_state_n_slide_compartment <- mpif_cell_state_expanded %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, compartment, cell_state, cell_type) %>%
#   tally() %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, compartment) %>%
#   mutate(nrel = n/sum(n)) %>%
#   ungroup()
# 
# ## fov tally helper function
# fov_tally <- . %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, fov_id) %>%
#   mutate(nrel = n/sum(n)) %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, fov_id, cell_type) %>%
#   mutate(nrel_ct = sum(nrel)) %>%
#   ungroup
# 
# ## slide tally helper function
# slide_tally <- . %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p) %>%
#   mutate(nrel = n/sum(n)) %>%
#   group_by(patient_id, tumor_supersite, tumor_subsite, therapy, sample_cd45p, cell_type) %>%
#   mutate(nrel_ct = sum(nrel)) %>%
#   ungroup() %>%
#   arrange(cell_type != "CD8+", desc(cell_type), nrel_ct) %>%
#   mutate(sample_cd45p = ordered(sample_cd45p, levels = unique(sample_cd45p)),
#          sort_short_x = "mpIF",
#          label_supersite = "Site",
#          label_therapy = "Rx",
#          label_mutsig = "Signature",
#          patient_id_short = str_sub(patient_id, 13, 15)) %>%
#   distinct(sample_cd45p, cell_state, .keep_all = T) %>%
#   arrange(sort_short_x, cell_type, cell_state) %>%
#   group_by(cell_type, cell_state, sort_short_x) %>%
#   mutate(rank_T.cell = row_number(sample_cd45p)) %>%
#   mutate(rank_T.cell = scales::rescale(rank_T.cell, c(-1, 1))) %>%
#   ungroup %>%
#   mutate(tumor_supersite = ordered(tumor_supersite, levels = rev(names(clrs$tumor_supersite))),
#          cell_state = ordered(cell_state, levels = names(clrs$cell_state)))
# 
# ## immune state composition fov lvl
# mpif_cell_state_n_imm <- mpif_cell_state_n %>%
#   filter(cell_type %in% c("CD68+", "CD8+")) %>%
#   fov_tally
# 
# ## immune state composition slide lvl
# mpif_cell_state_n_imm_slide <- mpif_cell_state_n_slide %>%
#   # filter(cell_type %in% c("CD68+", "CD8+")) %>%
#   slide_tally
# 
# ## immune state composition slide lvl per tumor
# mpif_cell_state_n_imm_slide_tumor <- mpif_cell_state_n_slide_compartment %>%
#   filter(compartment == "Tumor", therapy == "pre-Rx") %>%
#   slide_tally %>%
#   mutate(sort_short_x = compartment)
# 
# ## immune state composition slide lvl per stroma
# mpif_cell_state_n_imm_slide_stroma <- mpif_cell_state_n_slide_compartment %>%
#   filter(compartment == "Stroma", therapy == "pre-Rx") %>%
#   slide_tally %>%
#   mutate(sort_short_x = compartment)
# 
# ## CD68 state composition
# mpif_cell_state_n_imm_cd68 <- mpif_cell_state_n %>%
#   filter(cell_type %in% c("CD68+")) %>%
#   fov_tally
# 
# ## CD68 state composition slide lvl
# mpif_cell_state_n_imm_cd68_slide <- mpif_cell_state_n_slide %>%
#   filter(cell_type %in% c("CD68+")) %>%
#   slide_tally
# 
# ## CD8 state composition
# mpif_cell_state_n_imm_cd8 <- mpif_cell_state_n %>%
#   filter(cell_type %in% c("CD8+")) %>%
#   fov_tally
# 
# ## CD8 state composition slide lvl
# mpif_cell_state_n_imm_cd8_slide <- mpif_cell_state_n_slide %>%
#   filter(cell_type %in% c("CD8+")) %>%
#   slide_tally
# 
# # mpIF_cell_clrs <- c(clrs$cell_type, clrs$cluster_label$T.cell)
# # names(mpIF_cell_clrs) <- str_replace_all(names(mpIF_cell_clrs), " ", ".")

4.2 mpif vector ranks

# prim_ranks_cd45p_mpif <- filter(mpif_cell_state_n_imm_slide) %>%
#   filter((tumor_supersite %in% c("Adnexa")), cell_type == "CD8+", cell_state == "CD8+TOX-PD1-") %>%
#   mutate(rank = rank_T.cell) %>%
#   prim_ranks_wrapper("Adnexa")
# 
# meta_ranks_cd45p_mpif <- filter(mpif_cell_state_n_imm_slide) %>%
#   filter(!(tumor_supersite %in% c("Adnexa")), cell_type == "CD8+", cell_state == "CD8+TOX-PD1-") %>%
#   mutate(rank = rank_T.cell) %>%
#   prim_ranks_wrapper("Non-adnexa")
# 
# median_rank_tbl_cd45p_mpif <- bind_rows(distinct(prim_ranks_cd45p_mpif, median_rank, .keep_all = T),
#                                         distinct(meta_ranks_cd45p_mpif, median_rank, .keep_all = T)) %>%
#   median_rank_wrapper
# 
# rank_tbl_cd45p_mpif <- bind_rows(prim_ranks_cd45p_mpif, meta_ranks_cd45p_mpif) %>%
#   mutate(prim_meta = ifelse(tumor_supersite == "Ascites", "Ascites", prim_meta),
#          patient_id_short = ordered(patient_id_short, levels = levels(median_rank_tbl_cd45p_mpif$patient_id_short)))
# 
# vector_patient_lvls_cd45p_mpif <- levels(rank_tbl_cd45p$patient_id_short)
# vector_patient_lvls_cd45p_mpif[as.logical(1:length(vector_patient_lvls_cd45p_mpif) %% 2)] <- paste0(vector_patient_lvls_cd45p_mpif[as.logical(1:length(vector_patient_lvls_cd45p_mpif) %% 2)], "       ")
# 
# vector_rank_list_cd45p_mpif <- list(rank_tbl = rank_tbl_cd45p_mpif,
#                                median_rank_tbl = median_rank_tbl_cd45p_mpif,
#                                vector_patient_lvls = vector_patient_lvls_cd45p_mpif)
# 
# vector_plot_cd45p_mpif <- vector_patient_plot_wrapper(vector_rank_list_cd45p_mpif, c("#ff7f00", "#33a02c"))
# 
# 
# ## tumor compartment vectors
# prim_ranks_tumor_mpif <- filter(mpif_cell_state_n_imm_slide_tumor, sort_short_x == "Tumor", therapy == "pre-Rx", compartment == "Tumor") %>%
#   filter((tumor_supersite %in% c("Adnexa")), cell_type == "CD8+", cell_state == "CD8+TOX-PD1-") %>%
#   mutate(rank = rank_T.cell) %>%
#   prim_ranks_wrapper("Adnexa")
# 
# meta_ranks_tumor_mpif <- filter(mpif_cell_state_n_imm_slide_tumor, sort_short_x == "Tumor", therapy == "pre-Rx", compartment == "Tumor") %>%
#   filter(!(tumor_supersite %in% c("Adnexa")), cell_type == "CD8+", cell_state == "CD8+TOX-PD1-") %>%
#   mutate(rank = rank_T.cell) %>%
#   prim_ranks_wrapper("Non-adnexa")
# 
# median_rank_tbl_tumor_mpif <- bind_rows(distinct(prim_ranks_tumor_mpif, median_rank, .keep_all = T),
#                                         distinct(meta_ranks_tumor_mpif, median_rank, .keep_all = T)) %>%
#   median_rank_wrapper
# 
# rank_tbl_tumor_mpif <- bind_rows(prim_ranks_tumor_mpif, meta_ranks_tumor_mpif) %>%
#   mutate(prim_meta = ifelse(tumor_supersite == "Ascites", "Ascites", prim_meta),
#          patient_id_short = ordered(patient_id_short, levels = levels(median_rank_tbl_tumor_mpif$patient_id_short)))
# 
# vector_patient_lvls_tumor_mpif <- levels(rank_tbl_cd45p$patient_id_short)
# vector_patient_lvls_tumor_mpif[as.logical(1:length(vector_patient_lvls_tumor_mpif) %% 2)] <- paste0(vector_patient_lvls_tumor_mpif[as.logical(1:length(vector_patient_lvls_tumor_mpif) %% 2)], "       ")
# 
# vector_rank_list_tumor_mpif <- list(rank_tbl = rank_tbl_tumor_mpif,
#                                median_rank_tbl = median_rank_tbl_tumor_mpif,
#                                vector_patient_lvls = vector_patient_lvls_tumor_mpif)
# 
# vector_plot_tumor_mpif <- vector_patient_plot_wrapper(vector_rank_list_tumor_mpif, c("#ff7f00", "#33a02c"))
# 
# 
# ## stroma compartment vectors
# prim_ranks_stroma_mpif <- filter(mpif_cell_state_n_imm_slide_stroma, sort_short_x == "Stroma", therapy == "pre-Rx", compartment == "Stroma") %>%
#   filter((tumor_supersite %in% c("Adnexa")), cell_type == "CD8+", cell_state == "CD8+TOX-PD1-") %>%
#   mutate(rank = rank_T.cell) %>%
#   prim_ranks_wrapper("Adnexa")
# 
# meta_ranks_stroma_mpif <- filter(mpif_cell_state_n_imm_slide_stroma, sort_short_x == "Stroma", therapy == "pre-Rx", compartment == "Stroma") %>%
#   filter(!(tumor_supersite %in% c("Adnexa")), cell_type == "CD8+", cell_state == "CD8+TOX-PD1-") %>%
#   mutate(rank = rank_T.cell) %>%
#   prim_ranks_wrapper("Non-adnexa")
# 
# median_rank_tbl_stroma_mpif <- bind_rows(distinct(prim_ranks_stroma_mpif, median_rank, .keep_all = T),
#                                         distinct(meta_ranks_stroma_mpif, median_rank, .keep_all = T)) %>%
#   median_rank_wrapper
# 
# rank_tbl_stroma_mpif <- bind_rows(prim_ranks_stroma_mpif, meta_ranks_stroma_mpif) %>%
#   mutate(prim_meta = ifelse(tumor_supersite == "Ascites", "Ascites", prim_meta),
#          patient_id_short = ordered(patient_id_short, levels = levels(median_rank_tbl_stroma_mpif$patient_id_short)))
# 
# vector_patient_lvls_stroma_mpif <- levels(rank_tbl_cd45p$patient_id_short)
# vector_patient_lvls_stroma_mpif[as.logical(1:length(vector_patient_lvls_stroma_mpif) %% 2)] <- paste0(vector_patient_lvls_stroma_mpif[as.logical(1:length(vector_patient_lvls_stroma_mpif) %% 2)], "       ")
# 
# vector_rank_list_stroma_mpif <- list(rank_tbl = rank_tbl_stroma_mpif,
#                                median_rank_tbl = median_rank_tbl_stroma_mpif,
#                                vector_patient_lvls = vector_patient_lvls_stroma_mpif)
# 
# vector_plot_stroma_mpif <- vector_patient_plot_wrapper(vector_rank_list_stroma_mpif, c("#ff7f00", "#33a02c"))
# pcomp1 <- comp_plot_wrapper(mpif_cell_state_n_imm_slide, "mpIF", "n", x = "sample_cd45p", facet = F, fill = "cell_state") +
#   scale_y_continuous(expand = c(0, 0),
#                        breaks = c(0, 250000, 500000),
#                        limits = c(0, 500000),
#                        labels = c("", "250000", "500000")) +
#     expand_limits(y = c(0, 500000))
# 
# pcomp2 <- comp_plot_wrapper(mpif_cell_state_n_imm_slide, "mpIF", "nrel", x = "sample_cd45p", facet = F, fill = "cell_state") +
#   scale_y_continuous(expand = c(0, 0),
#                        breaks = c(0, 0.5, 1),
#                        limits = c(0, 1),
#                        labels = c("0", "50", "100")) +
#   expand_limits(y = c(0, 1))
# 
# 
# pcomp_grid_p_mpif <- plot_grid(pcomp1, pcomp2,
#                                comp_label_boxplot(filter(mpif_cell_state_n_imm_slide, cell_state == "CD8+TOX-PD1-"), x = "tumor_supersite", y = "rank_T.cell", facet = F, cts = "mpIF", ct = "CD8+", pvals = F) + remove_xaxis,
#                                vector_plot_cd45p + remove_guides,
#                                ncol = 1, align = "v",
#                                rel_heights = c(0.15, 0.15, 0.25, 0.45))
# 
# ggsave("_fig/002_cohort/002_sorted_comp_mpif.pdf", pcomp_grid_p_mpif, width = 3, height = 6)
# 

4.3 scRNA marker positivity

# # seu_cohort <- read_rds("/work/shah/isabl_data_lake/analyses/16/52/1652/cohort_merged.rdata")
# #
# # scrna_markers <- as_tibble(cbind(cell_id = colnames(seu_cohort), FetchData(seu_cohort, c("cell_type", "sample", "CD68", "PDCD1", "CD274", "TOX", "CD8A", "CD8B", "KRT8", "KRT19")))) %>%
# #   mutate(CD68_state = ifelse(CD68 > 0, "CD68+", "CD68-"),
# #          CD8_state = ifelse(CD8A > 0 | CD8B > 0, "CD8+", "CD8-"),
# #          panCK_state = ifelse(KRT8 > 0 | KRT19 > 0, "panCK+", "panCK-"),
# #          TOX_state = ifelse(TOX > 0, "TOX+", "TOX-"),
# #          PD1_state = ifelse(PDCD1 > 0, "PD1+", "PD1-"),
# #          PDL1_state = ifelse(CD274 > 0, "PDL1+", "PDL1-"))
# #
# # write_tsv(scrna_markers, "/work/shah/uhlitzf/data/SPECTRUM/freeze/v5/scrna_mpif_marker_expression.tsv")
# 
# scrna_markers <- read_tsv("/work/shah/uhlitzf/data/SPECTRUM/freeze/v5/scrna_mpif_marker_expression.tsv") %>%
#   filter(!(CD68_state == "CD68+" & CD8_state == "CD8+"),
#          !(CD68_state == "CD68+" & panCK_state == "panCK+"),
#          !(CD8_state == "CD8+" & panCK_state == "panCK+")) %>%
#   mutate(cell_type_sc = ifelse(CD68_state == "CD68+", "CD68+", ifelse(CD8_state == "CD8+", "CD8+", ifelse(panCK_state == "panCK+", "panCK+", "Other"))),
#          cell_state = case_when(
#            cell_type_sc == "CD8+" ~ paste0(CD8_state, TOX_state, PD1_state),
#            cell_type_sc == "CD68+" ~ paste0(CD68_state, PDL1_state),
#            cell_type_sc == "panCK+" ~ paste0(panCK_state, PDL1_state),
#            cell_type_sc == "Unknown" ~ "Unknown",
#            cell_type_sc == "Other" ~ "Other"
#          ))
# 
# markers_pos_frac_scrna_celltype <- seu_tbl_full %>%
#   # select(cell_id, sample) %>%
#   select(cell_id, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   left_join(select(scrna_markers, cell_id, cell_type_sc, cell_state), by = "cell_id") %>%
#   na.omit() %>%
#   group_by(sample, cell_type_sc, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   tally %>%
#   group_by(sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   mutate(nrel = n/sum(n)) %>%
#   select(sample, cell_type_sc, n, nrel, everything()) %>%
#   ungroup
# 
# markers_pos_frac_scrna_cellstate <- seu_tbl_full %>%
#   # select(cell_id, sample) %>%
#   select(cell_id, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   left_join(select(scrna_markers, cell_id, cell_type_sc, cell_state), by = "cell_id") %>%
#   na.omit() %>%
#   group_by(sample, cell_state, cell_type_sc, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   tally %>%
#   group_by(sample, cell_type_sc, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   mutate(nrel = n/sum(n)) %>%
#   select(sample, cell_type_sc, cell_state, n, nrel, everything()) %>%
#   ungroup
# 
# markers_pos_frac_scrna_gene <- seu_tbl_full %>%
#   # select(cell_id, sample) %>%
#   select(cell_id, sample, cell_type, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   left_join(select(scrna_markers, -sample, -cell_type), by = "cell_id") %>%
#   na.omit() %>%
#   select(-contains("state")) %>%
#   mutate(cell_type = str_replace_all(cell_type, "\\.", " ")) %>%
#   gather(gene, value, -cell_id, -sample, -cell_type, -tumor_supersite,
#          -consensus_signature, -patient_id_short, -sort_short_x, -cell_type_sc) %>%
#   mutate(gene_state = value > 0) %>%
#   group_by(gene, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x) %>%
#   mutate(n = sum(gene_state),
#          nrel = n/length(n)) %>%
#   ungroup %>%
#   distinct(gene, sample, tumor_supersite, consensus_signature, patient_id_short, sort_short_x, n, nrel) %>%
#   select(sample, gene, n, nrel, everything())

4.4 mpIF x scRNA correlation

4.4.1 CD45+ cell type correlation

# markers_pos_scrna_mpif <- mpif_cell_state_n_slide_compartment %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel) %>%
#   group_by(sample, cell_type_sc, compartment) %>%
#   summarise(n_mpif = sum(n_mpif), nrel_mpif = sum(nrel_mpif)) %>%
#   ungroup() %>%
#   left_join(markers_pos_frac_scrna_celltype, by = c("sample", "cell_type_sc")) %>%
#   filter(sort_short_x == "CD45+",
#          cell_type_sc %in% c("CD8+", "CD68+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_type_sc~compartment, scales = "free"),
#   stat_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "spearman", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2)

4.4.2 CD45+ cell state correlation

# markers_pos_scrna_mpif_state <- mpif_cell_state_n_slide_compartment %>%
#   group_by(sample_cd45p, compartment, cell_type) %>%
#   mutate(nrel_state = n/sum(n)) %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel_state, cell_state) %>%
#   left_join(markers_pos_frac_scrna_cellstate, by = c("sample", "cell_type_sc", "cell_state")) %>%
#   filter(sort_short_x == "CD45+",
#          cell_state %in% c("CD8+TOX-PD1-", "CD8+TOX+PD1+", "CD68+PDL1-", "CD68+PDL1+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_state~compartment, scales = "free", ncol = 4),
#   geom_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "spearman", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif_state) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif_state) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2, ncol = 1)

4.4.3 CD45- cell type correlation

# markers_pos_scrna_mpif_cd45n <- mpif_cell_state_n_slide_compartment %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel) %>%
#   mutate(sample = str_replace_all(sample, "CD45P", "CD45N")) %>%
#   group_by(sample, cell_type_sc, compartment) %>%
#   summarise(n_mpif = sum(n_mpif), nrel_mpif = sum(nrel_mpif)) %>%
#   ungroup() %>%
#   left_join(markers_pos_frac_scrna_celltype, by = c("sample", "cell_type_sc")) %>%
#   filter(sort_short_x == "CD45-",
#          cell_type_sc %in% c("panCK+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_type_sc~compartment, scales = "free"),
#   stat_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "pearson", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2)

4.4.4 CD45- cell state correlation

# markers_pos_scrna_mpif_state_cd45n <- mpif_cell_state_n_slide_compartment %>%
#   group_by(sample_cd45p, compartment, cell_type) %>%
#   mutate(nrel_state = n/sum(n)) %>%
#   select(sample = sample_cd45p, cell_type_sc = cell_type,
#          compartment, n_mpif = n, nrel_mpif = nrel_state, cell_state) %>%
#   mutate(sample = str_replace_all(sample, "CD45P", "CD45N")) %>%
#   left_join(markers_pos_frac_scrna_cellstate, by = c("sample", "cell_type_sc", "cell_state")) %>%
#   filter(sort_short_x == "CD45-",
#          cell_state %in% c("panCK+PDL1-", "panCK+PDL1+"),
#          compartment %in% c("Stroma", "Tumor"))
# 
# common_layers <- list(
#   facet_wrap(cell_state~compartment, scales = "free", ncol = 4),
#   geom_smooth(aes(nrel, nrel_mpif), method = "lm", color = "black"),
#   stat_cor(aes(nrel, nrel_mpif), method = "pearson", color = "black"),
#   labs(x = "Fraction in scRNA",
#        y = "Fraction in mpIF")
# )
# 
# p1 <- ggplot(markers_pos_scrna_mpif_state_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = tumor_supersite)) +
#   scale_color_manual(values = clrs$tumor_supersite) +
#   common_layers
# 
# p2 <- ggplot(markers_pos_scrna_mpif_state_cd45n) +
#   geom_point(aes(nrel, nrel_mpif, color = consensus_signature)) +
#   scale_color_manual(values = clrs$consensus_signature) +
#   common_layers
# 
# plot_grid(p1, p2, ncol = 1)